import QtQuick 2.14
import QtQuick.Controls 2.14
import QtQuick.Layouts 1.14

Page {
    id: settingsPage
    title: "系统设置"
    
    property var settings: ({})
    
    Component.onCompleted: {
        loadSettings()
    }
    
    function loadSettings() {
        if (typeof qmlBridge !== 'undefined') {
            settings = qmlBridge.getSystemSettings()
        }
    }
    
    function saveSettings() {
        if (typeof qmlBridge !== 'undefined') {
            var newSettings = {
                "autoStartDevice": autoStartSwitch.checked,
                "samplingInterval": samplingIntervalSpinBox.value,
                "batteryWarningLevel": batteryWarningSpinBox.value,
                "dataRetentionDays": dataRetentionSpinBox.value,
                "enableAuditLog": auditLogSwitch.checked,
                "language": languageComboBox.currentText
            }
            
            if (qmlBridge.saveSystemSettings(newSettings)) {
                statusLabel.text = "设置保存成功"
                statusLabel.color = "green"
            } else {
                statusLabel.text = "设置保存失败"
                statusLabel.color = "red"
            }
        }
    }
    
    ScrollView {
        anchors.fill: parent
        anchors.margins: 20
        
        ColumnLayout {
            width: parent.width
            spacing: 20
            
            // 标题
            Text {
                text: "系统设置"
                font.pixelSize: 24
                font.bold: true
                Layout.fillWidth: true
            }
            
            // 设备设置组
            GroupBox {
                title: "设备设置"
                Layout.fillWidth: true
                
                ColumnLayout {
                    anchors.fill: parent
                    spacing: 10
                    
                    RowLayout {
                        Text {
                            text: "自动启动设备:"
                            Layout.preferredWidth: 150
                        }
                        Switch {
                            id: autoStartSwitch
                            checked: settings.autoStartDevice || false
                        }
                    }
                    
                    RowLayout {
                        Text {
                            text: "采样间隔 (秒):"
                            Layout.preferredWidth: 150
                        }
                        SpinBox {
                            id: samplingIntervalSpinBox
                            from: 10
                            to: 3600
                            value: settings.samplingInterval || 60
                        }
                    }
                    
                    RowLayout {
                        Text {
                            text: "电池警告电量 (%):"
                            Layout.preferredWidth: 150
                        }
                        SpinBox {
                            id: batteryWarningSpinBox
                            from: 5
                            to: 50
                            value: settings.batteryWarningLevel || 20
                        }
                    }
                }
            }
            
            // 数据设置组
            GroupBox {
                title: "数据设置"
                Layout.fillWidth: true
                
                ColumnLayout {
                    anchors.fill: parent
                    spacing: 10
                    
                    RowLayout {
                        Text {
                            text: "数据保留天数:"
                            Layout.preferredWidth: 150
                        }
                        SpinBox {
                            id: dataRetentionSpinBox
                            from: 1
                            to: 365
                            value: settings.dataRetentionDays || 30
                        }
                    }
                    
                    RowLayout {
                        Text {
                            text: "启用审计日志:"
                            Layout.preferredWidth: 150
                        }
                        Switch {
                            id: auditLogSwitch
                            checked: settings.enableAuditLog !== false
                        }
                    }
                }
            }
            
            // 系统设置组
            GroupBox {
                title: "系统设置"
                Layout.fillWidth: true
                
                ColumnLayout {
                    anchors.fill: parent
                    spacing: 10
                    
                    RowLayout {
                        Text {
                            text: "语言:"
                            Layout.preferredWidth: 150
                        }
                        ComboBox {
                            id: languageComboBox
                            model: ["简体中文", "English"]
                            currentIndex: (settings.language === "en_US") ? 1 : 0
                        }
                    }
                }
            }
            
            // 备份恢复组
            GroupBox {
                title: "备份与恢复"
                Layout.fillWidth: true
                
                ColumnLayout {
                    anchors.fill: parent
                    spacing: 10
                    
                    RowLayout {
                        Button {
                            text: "系统备份"
                            onClicked: {
                                if (typeof qmlBridge !== 'undefined') {
                                    var backupPath = "backup_" + new Date().toISOString().slice(0,10) + ".bak"
                                    qmlBridge.performSystemBackup(backupPath)
                                }
                            }
                        }
                        
                        Button {
                            text: "恢复备份"
                            onClicked: {
                                // 这里应该打开文件选择对话框
                                if (typeof qmlBridge !== 'undefined') {
                                    qmlBridge.restoreSystemBackup("backup.bak")
                                }
                            }
                        }
                    }
                }
            }
            
            // 操作按钮
            RowLayout {
                Layout.fillWidth: true
                
                Button {
                    text: "保存设置"
                    highlighted: true
                    onClicked: saveSettings()
                }
                
                Button {
                    text: "重置默认"
                    onClicked: {
                        autoStartSwitch.checked = true
                        samplingIntervalSpinBox.value = 60
                        batteryWarningSpinBox.value = 20
                        dataRetentionSpinBox.value = 30
                        auditLogSwitch.checked = true
                        languageComboBox.currentIndex = 0
                    }
                }
                
                Item {
                    Layout.fillWidth: true
                }
            }
            
            // 状态标签
            Text {
                id: statusLabel
                text: ""
                Layout.fillWidth: true
            }
        }
    }
}